package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.TranslateUtil;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.MrCompany;
import com.ruoyi.system.service.IMrCompanyService;

/**
 * 公司Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-18
 */
@Service
public class MrCompanyServiceImpl implements IMrCompanyService
{
    @Autowired
    private MrCompanyMapper mrCompanyMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    @Autowired
    private SysRoleMapper roleMapper;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private SysUserRoleMapper userRoleMapper;

    /**
     * 查询公司
     *
     * @param comId 公司主键
     * @return 公司
     */
    @Override
    public MrCompany selectMrCompanyByComId(Long comId)
    {
        return mrCompanyMapper.selectMrCompanyByComId(comId);
    }

    /**
     * 查询公司列表
     *
     * @param mrCompany 公司
     * @return 公司
     */
    @Override
    public List<MrCompany> selectMrCompanyList(MrCompany mrCompany)
    {
        return mrCompanyMapper.selectMrCompanyList(mrCompany);
    }

    /**
     * 新增公司
     *
     * @param mrCompany 公司
     * @return 结果
     */
    @Override
    public int insertMrCompany(MrCompany mrCompany)
    {
        mrCompany.setCreateTime(DateUtils.getNowDate());
        int count = mrCompanyMapper.insertMrCompany(mrCompany);

        //新增公司时自动创建区域根节点 && 部门根节点 && 管理员角色 && 管理员用户
        if(count > 0){

            SysDept sysDept = new SysDept();
            sysDept.setParentId(0L);
            sysDept.setAncestors("0");
            sysDept.setDeptName(mrCompany.getComName());
            sysDept.setOrderNum(0);
            sysDept.setStatus("0");
            sysDept.setDelFlag("0");
            sysDept.setCreateBy(mrCompany.getCreateBy());
            sysDept.setCreateTime(DateUtils.getNowDate());
            sysDept.setComId(mrCompany.getComId());
            deptMapper.insertDept(sysDept);

            String key = TranslateUtil.getPinYinHeadChar(mrCompany.getComName());

            //只添加角色,菜单权限由管理员在页面开通
            SysRole role = new SysRole();
            role.setRoleName(key + "管理员");
//            role.setRoleKey(key + mrCompany.getComId() + "admin");
            role.setRoleKey("");
            role.setRoleSort("1");
            role.setDataScope("1");
            role.setDeptCheckStrictly(true);
            role.setDeptCheckStrictly(true);
            role.setStatus("0");
            role.setDelFlag("0");
            role.setCreateBy(mrCompany.getCreateBy());
            role.setCreateTime(DateUtils.getNowDate());
            role.setComId(mrCompany.getComId());
            role.setLockFlag("1");
            roleMapper.insertRole(role);

            //添加用户
            SysUser user = new SysUser();
            user.setUserName(key + mrCompany.getComId() + "admin");
            user.setNickName(key + "管理员");
            user.setDeptId(sysDept.getDeptId());
            user.setSex("1");
            user.setPassword("$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2");
            user.setStatus("0");
            user.setDelFlag("0");
            user.setCreateBy(mrCompany.getCreateBy());
            user.setCreateTime(DateUtils.getNowDate());
            user.setComId(mrCompany.getComId());
            userMapper.insertUser(user);

            List<SysUserRole> list = new ArrayList<SysUserRole>();
            SysUserRole ur = new SysUserRole();
            ur.setUserId(user.getUserId());
            ur.setRoleId(role.getRoleId());
            list.add(ur);
            userRoleMapper.batchUserRole(list);
        }

        return count;
    }

    /**
     * 修改公司
     *
     * @param mrCompany 公司
     * @return 结果
     */
    @Override
    public int updateMrCompany(MrCompany mrCompany)
    {
        mrCompany.setUpdateTime(DateUtils.getNowDate());
        return mrCompanyMapper.updateMrCompany(mrCompany);
    }

    /**
     * 批量删除公司
     *
     * @param comIds 需要删除的公司主键
     * @return 结果
     */
    @Override
    public int deleteMrCompanyByComIds(Long[] comIds)
    {
        return mrCompanyMapper.deleteMrCompanyByComIds(comIds);
    }

    /**
     * 删除公司信息
     *
     * @param comId 公司主键
     * @return 结果
     */
    @Override
    public int deleteMrCompanyByComId(Long comId)
    {
        return mrCompanyMapper.deleteMrCompanyByComId(comId);
    }
}
